package the.offer.CodingInterviewGuide.Charpter_1_Stack_and_Queue;


import the.offer.DataStructure.Impl.ArrayStack;
import the.offer.DataStructure.Interface.Stack;

public class SortStackByStack {

    public static void sortStackByStack(Stack<Integer> stack) {
        int count = 1;
        Stack<Integer> help = new ArrayStack<>();
        System.out.println(stack.toString());
        while (!stack.isEmpty()) {
            Integer cur = stack.pop();
            while (!help.isEmpty() && cur < help.peek()) {
                stack.push(help.pop());
            }
            help.push(cur);
            System.out.println(count++);
            System.out.println("stack : " + stack.toString());
            System.out.println("help : " + help.toString());
        }
        while (!help.isEmpty()) {
            stack.push(help.pop());
        }
        System.out.println(stack.toString());
    }

    public static void main(String[] args) {
        ArrayStack<Integer> stack = new ArrayStack<>();
        stack.push(2);
        stack.push(3);
        stack.push(1);
        sortStackByStack(stack);
    }
}
